using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;
using System.IO;
using XLCS.Common;
using System.Net;
using XLCS.Xml;
using System.Xml;
using XLCS.Webs;
using System.Reflection;

namespace SqlCeViewer.FileFoderUI
{
    public partial class LiveUpdate : BaseSubForm
    {
        public LiveUpdate()
        {
            InitializeComponent();


        }

        private void butHistory_Click(object sender, EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string tempFile = "C:\\TempLiveFile.xml";
            string tempUpdateExe = "C:\\TempUpdate.rar";

            try
            {
                StringBuilder detail = GetOnlineResources.GetOnlineHtmlFromURL(Properties.Settings.Default.LiveUpdateAddress);

                XmlDocument xml = new XmlDocument();
                xml.LoadXml(detail.ToString());
                xml.Save(tempFile);

                string result = string.Empty;

                OldSelfDefined.retriveObjForFirst(tempFile, "Version", out result);

                int nextVersion = Convert.ToInt32(result.Replace('.', '2'));
                int curVersion = Convert.ToInt32(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace('.', '2'));
                if (nextVersion > curVersion)
                {
                    if (System.Windows.Forms.MessageBox.Show(String.Format("New version {0} existed ,do you want to update ?", result), "Notify", System.Windows.Forms.MessageBoxButtons.OKCancel,
                        System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        string updateAddress = string.Empty;
                        OldSelfDefined.retriveObjForFirst(tempFile, "Address", out updateAddress);
                        if (string.IsNullOrEmpty(updateAddress))
                        {
                            MessageBox.Show("Invalid address ,update will cancel");
                            return;
                        }

                        updateAddress = Properties.Settings.Default.LiveUpdateAddress + "\\" + updateAddress;

                        using (WebClient Client = new WebClient())
                        {
                            Client.DownloadFile(updateAddress, tempUpdateExe);
                        }

                        MessageBox.Show("Get Update file successful ");
                    }
                }
                else
                {
                    MessageBox.Show("Your version is up-to-date");
                }
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);

            }
            finally
            {
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                if (File.Exists(tempUpdateExe))
                {
                    File.Delete(tempUpdateExe);
                }
            }

        }
    }
}